#include "viewusrsdialog.h"
#include "ui_viewusrsdialog.h"
#include<QIcon>
#include<QDebug>
#include<QMessageBox>
ViewUsrsDialog::ViewUsrsDialog(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ViewUsrsDialog)
{
    ui->setupUi(this);
    setFixedSize(857,537);
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowModality(Qt::ApplicationModal);
    setWindowIcon(QIcon(":/img/application.png"));
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableWidget->setColumnWidth(0, 200);
    ui->tableWidget->setColumnWidth(1, 130);
    ui->tableWidget->setColumnWidth(2, 130);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->tableWidget->setAlternatingRowColors(true);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);        //设置不可编辑

    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed); // 设为固定模式
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(60);                // 每行高度 40 像素

}

ViewUsrsDialog::~ViewUsrsDialog()
{
    delete ui;
}

void ViewUsrsDialog::on_searchBtn_clicked()
{
    QString name=ui->usrSearchEdit->text().trimmed();
    m_searchName=name;
    if(!validateString(name)){
        QMessageBox::critical(this,"critical","用户名只能由英文大小写字母以及数字和下划线组成，且最大长度为31！");
        ui->usrSearchEdit->clear();
        m_searchName="";
        return;
    }
    if(!name.isEmpty()){
        emit searchName(name);
        ui->usrSearchEdit->clear();
    }else{
        QMessageBox::critical(this,"critical","用户名为空，请重新输入！");
        m_searchName="";
    }
}

//校验用户名
bool ViewUsrsDialog::validateString(const QString &input) {
    QRegularExpression regex("^[A-Za-z0-9_]{1,31}$");
    QRegularExpressionMatch match = regex.match(input);
    if (!match.hasMatch()) {
        return false;
    }
    return true;
}

void ViewUsrsDialog::getSearchData(const QList<UserInfo> &usrList)
{
    ui->tableWidget->clearContents();  // 清除单元格内容，但保留表头和结构
    ui->tableWidget->setRowCount(0);   // 设置为0行，从而移除所有行

    for(int i=0;i<usrList.size();i++){
        ui->tableWidget->insertRow(i);       // 插入新行
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(usrList[i].name));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(usrList[i].online==1?"是":"否"));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(usrList[i].isFriend==1?"是":"否"));
        QWidget *widget = new QWidget;
        QPushButton *btnDel=nullptr;
        QPushButton *btnAdd=nullptr;
        if(usrList[i].isFriend==1){
            btnDel= new QPushButton(QIcon(":/img/delete.png"),"", this);
            btnAdd = new QPushButton(QIcon(":/img/addFriendgrey.png"),"", this);
            btnAdd->setEnabled(false);
        }else{
            btnDel= new QPushButton(QIcon(":/img/deletegrey.png"),"", this);
            btnAdd = new QPushButton(QIcon(":/img/addFriend.png"),"", this);
            btnDel->setEnabled(false);
        }
        btnDel->setFixedSize(35, 35);
        btnAdd->setFixedSize(35, 35);
        btnDel->setToolTip("删除该用户");
        btnAdd->setToolTip("添加该用户");
        connect(btnDel,&QPushButton::clicked,this,[=](){
            emit deleteUsr(usrList[i].name);
        });
        connect(btnAdd,&QPushButton::clicked,this,[=](){
            emit addUsr(usrList[i].name);
        });

        // 布局按钮
        QHBoxLayout *layout = new QHBoxLayout(widget);
        layout->addWidget(btnDel);
        layout->addWidget(btnAdd);
        widget->setLayout(layout);

        ui->tableWidget->setCellWidget(i, 3, widget);
    }
}

void ViewUsrsDialog::getFlushSignal()
{
    if(!validateString(m_searchName)){
        qDebug()<<"用户名只能由英文大小写字母以及数字和下划线组成，且最大长度为31！";
        return;
    }
    if(!m_searchName.isEmpty()){
        emit searchName(m_searchName);
    }
}
